package guis;

import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import classes.Tableble;

public class TablePanel< T extends Tableble > extends JPanel
{
	private static final long	serialVersionUID	= -2466625899855785356L;

	private JScrollPane scrollTable;
	private JTable table;

	public TablePanel( int x, int y, int width, int height )
	{
		this.scrollTable = new JScrollPane();	
		this.table = new JTable();

		scrollTable.setViewportView( table );
		table.getTableHeader().setReorderingAllowed( false );
		table.setAutoResizeMode( JTable.AUTO_RESIZE_NEXT_COLUMN );
		table.getColumnModel().setColumnMargin( 10 );
		this.setLayout( new GridLayout( 1, 0 ) );
		this.add( scrollTable );
		this.setBounds( x, y, width, height );
		//table.removeMouseListener(  )
	}

	public void setHeader( T tableble )
	{
		String[] columns = tableble.getHead();
		table.setModel( new DefaultTableModel( new Object[][] {}, columns ) );
		
		table.getColumnModel().removeColumn( table.getTableHeader().getColumnModel().getColumn( 0 ) );
	}
	
	public void addRow( T tableble )
	{
		Object[] columns = tableble.getRow();
		DefaultTableModel dtm = ( DefaultTableModel ) table.getModel();
		dtm.addRow( columns );
	}
	
	public void remove()
	{
		if( table.getSelectedRowCount() > 0 )
		{
			DefaultTableModel dtm = ( DefaultTableModel ) table.getModel();
			dtm.removeRow( table.getSelectedRow() );
		}
	}
	
	public JTable getTable()
	{
		return table;
	}
	
	public void hideCollumn( int columnIndex )
	{
		table.getColumnModel().removeColumn( table.getTableHeader().getColumnModel().getColumn( columnIndex ) );
	}
}
